package de.tapps.brainstorming;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Movie;
import android.os.AsyncTask;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
/**
 * Reads the topics and shows them in a list
 * 
 * @author Timo
 * Decrypted - ok
 */
public class TopicList extends AsyncTask<String, Integer, String> {
	private Storage storage;
	private Crypter crypter;
	private MainActivity activity = new MainActivity();
	private ImageTest gui = new ImageTest();

	public TopicList(MainActivity act){
		activity = act;
		crypter = new Crypter(activity);
		storage = new Storage(activity);

	}
	
	
	@Override
	protected String doInBackground(String... arg0) {
		  
		activity.runOnUiThread(new Runnable() {
			public void run() {
				Intent intent = new Intent(activity, ImageTest.class);
		        activity.startActivityForResult(intent,1);
		        activity.closeit = true;
			}});
				URL url = null;

				ArrayList<String> listEintraege = new ArrayList<String>();
				ArrayList<Integer> listIDs = new ArrayList<Integer>();
				ArrayList<String> listDate = new ArrayList<String>();
				try {
					url = new URL(
							"http://t-apps-online.de/BrainStorming/getTopicListBeta.php?lang="+storage.getLanguage()+"&order="+activity.order);
					String readLine = null;
					int counter = 0;
					BufferedReader buffReader = new BufferedReader(
							new InputStreamReader(url.openStream()));
					
					while ((readLine = buffReader.readLine()) != null) {
						if (counter % 3 == 0) {
							listEintraege.add(crypter.decrypt(readLine));
						}
						if (counter % 3 == 1){
							listIDs.add(Integer.valueOf(readLine));
						}
						if (counter % 3 == 2) {
							listDate.add(readLine);
						}
						counter++;
					}
				} catch (MalformedURLException me) {
					me.printStackTrace();
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
				activity.topiclist = listEintraege.toArray(new String[listEintraege
						.size()]);
				activity.topicIDs = listIDs.toArray(new Integer[listIDs.size()]);
				activity.topicDates = listDate.toArray(new String[listDate.size()]);
				activity.topicsWithDate = new String[activity.topiclist.length];
				
				for (int i=0;i<activity.topiclist.length;i++){
					activity.topicsWithDate[i] = activity.topiclist[i]+"\n					veröffentlicht: "+activity.topicDates[i];
				}
				
				activity.runOnUiThread(new Runnable() {
					public void run() {
						activity.finishActivity(1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
								activity, R.layout.custom_list_answers,
								activity.topicsWithDate);
						activity.setListAdapter(adapter);
						
					}
				});
			
	
		return null;
	}
	

	
}
